#include "Resource.h"
#include "String.h"
#include "SharedPtr.h"
#include "ResourceManager.h"
#include "Database/DatabaseEnv.h"
#include "StringConverter.h"
#include "NGLog.h"
#include "Character.h"
#include "CharManager.h"
#include "Items.h"
#include "ItemManager.h"
#include "WorldPacket.h"
#include "ScriptMgr.h"
#include "WSSocket.h"
#include "Opcodes.h"
#include "Users.h"
#include "UserManager.h"
#include "MD5.h"
#include "Towns.h"
#include "OpcodesEx.h"
#include "Mails.h"
#include "MailManager.h"
#include "ChannelManager.h"
#include "Message.h"
#include "WSSocketManager.h"
#include "ResourceEventEnum.h"
#include "NoticeManager.h"
#include "GameThreadLogic.h"
#include "GatewayOpcodes.h"
#include "json/json.h"
#include "StringScreen.h"
#include "ProtocolDealEnums.h"
#include "CenterMatchScriptWorldProcess.h"
#include "GameDefine.h"
#include LOG_MGR_PATH
#include LOGIC_MGR_PATH
#include PROTOCOLS_MGR_PATH
#include XMLDATA_MGR_PATH
#include "GameGoodsManager.h"
#include "GlobalCommonFunctons.h"
#include "GameDataEnum.h"
#include "subgames/Morra.h"
#include "CenterBankManager.h"
// #include "GoldCoronal.h"
#include "DataTransferManager.h"
#include "VipCardManager.h"
#include "PubDataManager.h"
#include "Tools.h"
// #include "CenterBattleManager.h"
// #include "CenterBattleLogManager.h"
// #include "CenterFriendManager.h"
#include "CenterHappyFarmManager.h"
#include "CRUsersManager.h"
#include "showip.h"
#include "AIInterface.h"


using namespace AIScript;

//CMSS_CenterMatch_PublicMatchList = 0x2700,				//比赛列表
bool  script_world_process_CM_MatchList(ResourcePtr & sock, WorldPacket & packet)
{
	WSSocketPtr socket = sock;
	char cCode[14];
	if (!ChkSocketPacket(socket, packet, cCode))
		return true;

	CharPtr pChr = socket->getCurrentCharacter();
	if (pChr.isNull())
		return true;
	uint8 type = 0;
	packet >> type;

	sCenterMatchMgr.SendCenterMatchListToPlayer(pChr,type);

	return true;
}

//CMSS_CenterMatch_MatchInfo = 0x2702,				//比赛信息
bool SERVER_DECL script_world_process_CM_MatchInfo(ResourcePtr & sock, WorldPacket & packet)
{
	WSSocketPtr socket = sock;
	char cCode[14];
	if (!ChkSocketPacket(socket, packet, cCode))
		return true;

	CharPtr pChr = socket->getCurrentCharacter();
	if (pChr.isNull())
		return true;
	uint32 cm_id = 0;
	packet >> cm_id;


	return true;
}

//CMSS_CenterMatch_EnrollmentForm = 0x2703,			//报名列表
bool SERVER_DECL script_world_process_CM_EnrollmentForm(ResourcePtr & sock, WorldPacket & packet)
{
	WSSocketPtr socket = sock;
	char cCode[14];
	if (!ChkSocketPacket(socket, packet, cCode))
		return true;

	CharPtr pChr = socket->getCurrentCharacter();
	if (pChr.isNull())
		return true;
	uint32 cm_id = 0;
	packet >> cm_id;


	return true;
}

//CMSS_CenterMatch_SignUp = 0x2704,					//报名
bool  script_world_process_CM_SignUp(ResourcePtr & sock, WorldPacket & packet)
{
	WSSocketPtr socket = sock;
	char cCode[14];
	if (!ChkSocketPacket(socket, packet, cCode))
		return true;

	CharPtr pChr = socket->getCurrentCharacter();
	if (pChr.isNull())
		return true;
	uint32 cm_id = 0;

	packet >> cm_id;
	String join_code;
	GetPacketString<uint8>(&packet, join_code);

	if (cm_id == 0 && join_code != "")
	{
		CenterMatch cm;
		sCenterMatchMgr.GetCenterMatch(join_code, &cm);
		sCenterMatchMgr.PlayerEnterMatch(pChr, cm.cm_id, true);
	}
	else
	{
		sCenterMatchMgr.PlayerEnterMatch(pChr, cm_id);
	}
	
	return true;
}

//CMSS_CenterMatch_ReviewSignUp = 0x2705,					//审核报名
bool script_world_process_CM_ReviewSignUp(ResourcePtr & sock, WorldPacket & packet)
{
	WSSocketPtr socket = sock;
	char cCode[14];
	if (!ChkSocketPacket(socket, packet, cCode))
		return true;

	CharPtr pChr = socket->getCurrentCharacter();
	if (pChr.isNull())
		return true;
	uint32 cm_id = 0;
	uint8 agreeOrfalse = 0;
	uint32 chr_id = 0;
	packet >> cm_id >> chr_id >>  agreeOrfalse;
	
	uint32 pid = 0;
	UserPtr pUser = socket->getCurrentUser();
	if (pUser.isNull() == false)
	{
		pid = pUser->getUInt32Field("platform_id");
	}

	CenterMatch cm;

	sCenterMatchMgr.GetCenterMatch(cm_id, &cm);

	sCenterMatchMgr.DealPlayerEnterCenterMatch(pid,cm.gz_id,cm_id, chr_id,agreeOrfalse);

	return true;
}

//CMSS_CenterMatch_PlayerInfoListInCenter = 0x2706,					//玩家信息
bool  script_world_process_CM_PlayerInfoList(ResourcePtr & sock, WorldPacket & packet)
{
	WSSocketPtr socket = sock;
	char cCode[14];
	if (!ChkSocketPacket(socket, packet, cCode))
		return true;

	CharPtr pChr = socket->getCurrentCharacter();
	if (pChr.isNull())
		return true;
	uint32 cm_id = 0;
	uint8 agreeOrfalse = 0;
	uint32 chr_id = 0;
	packet >> cm_id >> chr_id >> agreeOrfalse;

	uint32 pid = 0;
	UserPtr pUser = socket->getCurrentUser();
	if (pUser.isNull() == false)
	{
		pid = pUser->getUInt32Field("platform_id");
	}

	CenterMatch cm;

	sCenterMatchMgr.GetCenterMatch(cm_id, &cm);

	
	
	sCenterMatchMgr.SendCenterMatchPlayerListToPlayer(pChr, cm_id);


	return true;
}

//CMSS_CenterMatch_CreateMatch = 0x2707,						//创建比赛
bool  script_world_process_CM_CreateMatch(ResourcePtr & sock, WorldPacket & packet)
{
	WSSocketPtr socket = sock;
	char cCode[14];
	if (!ChkSocketPacket(socket, packet, cCode))
		return true;

	CharPtr pChr = socket->getCurrentCharacter();
	if (pChr.isNull())
		return true;
	
	String room_join = "";

	GetPacketString<uint16>(&packet, room_join);
	

	sCenterMatchMgr.CreateCenterMatch(pChr, room_join);

	return true;
}

//CMSS_CenterMatch_EnterMatch = 0x2708,						//加入比赛
bool  script_world_process_CM_EnterGameMatch(ResourcePtr & sock, WorldPacket & packet)
{
	WSSocketPtr socket = sock;
	char cCode[14];
	if (!ChkSocketPacket(socket, packet, cCode))
		return true;

	CharPtr pChr = socket->getCurrentCharacter();
	if (pChr.isNull())
		return true;

	uint32 cm_id = 0;
	packet >> cm_id;
	//String join_code;
	//GetPacketString<uint8>(&packet, join_code);

	Log.Debug("script_world_process_CM_EnterGameMatch", "0x2708 cm_id:%d ",cm_id);

	CenterMatch cm;
	sCenterMatchMgr.GetCenterMatch(cm_id, &cm);
	
	CenterMatchManager::MyStruct *cml2 = NULL;
	CenterMatchLogic* cml = sCenterMatchMgr.FindMatchById(cm.cm_id, cml2);
	Log.Debug("script_world_process_CM_EnterGameMatch", "result[%x]", cml);
	if (cml)
	{
		UserPtr pUser = socket->getCurrentUser();
		cml->PlayerJoinMatch(pUser);
		Log.Debug("script_world_process_CM_EnterGameMatch", "0x2708 cm_id:%d  player[%d]", cml->m_cm_id,cml->GetMatchPlayerNumInfo(enCenterMatchPlayerStatus_Free));

		return true;
	}
	else if(cm.cm_id != 0)
	{
		pChr->setUInt32Field("hp", 1);
		pChr->SaveDB();
		return true;
	}
	return true;
}

//CMSS_CenterMatch_PlayerEnterMatchList = 0x2709,						//已经入比赛列表
bool script_world_process_CM_GiveEnterGameMatch(ResourcePtr & sock, WorldPacket & packet)
{
	WSSocketPtr socket = sock;
	char cCode[14];
	if (!ChkSocketPacket(socket, packet, cCode))
		return true;

	CharPtr pChr = socket->getCurrentCharacter();
	if (pChr.isNull())
		return true;

	sCenterMatchMgr.SendCenterMatchPlayerEnterList(pChr);


	return true;
}

//CMSS_CenterMatch_PlayerGiveUpMatch = 0x270A,						//玩家退赛
bool script_world_process_CM_PlayerGiveUP(ResourcePtr & sock, WorldPacket & packet)
{
	WSSocketPtr socket = sock;
	char cCode[14];
	if (!ChkSocketPacket(socket, packet, cCode))
		return true;

	CharPtr pChr = socket->getCurrentCharacter();
	if (pChr.isNull())
		return true;

	uint32 cm_id = 0;

	packet >> cm_id;

	CenterMatch cm;
	
	sCenterMatchMgr.PlayerGiveUpEnterMatch(pChr, cm_id);
	
	return true;
}

//CMSS_CenterMatch_CenterMatchDissolve = 0x270B,						//解散比赛
bool SERVER_DECL script_world_process_CM_DissolveMatch(ResourcePtr & sock, WorldPacket & packet)
{
	WSSocketPtr socket = sock;
	char cCode[14];
	if (!ChkSocketPacket(socket, packet, cCode))
		return true;

	CharPtr pChr = socket->getCurrentCharacter();
	if (pChr.isNull())
		return true;

	uint32 cm_id;
	packet >> cm_id;

	sCenterMatchMgr.FZDissolveCenterMatch(cm_id, pChr);

	return true;
}

// CMSS_CenterMatch_CenterMatchWatch = 0x270C,							//比赛观战
bool SERVER_DECL script_world_process_CM_WatchMatch(ResourcePtr & sock, WorldPacket & packet)
{
	WSSocketPtr socket = sock;
	char cCode[14];
	if (!ChkSocketPacket(socket, packet, cCode))
		return true;

	CharPtr pChr = socket->getCurrentCharacter();
	if (pChr.isNull())
		return true;

	uint32 cm_id;
	uint32 channel_id;
	packet >> cm_id >> channel_id;
	
	Log.Debug("script_world_process_CM_WatchMatch","char[%u] cm_id[%u] channel_id[%u]", pChr->getHandle(), cm_id, channel_id);
	sCenterMatchMgr.PlayerWatchMatch(pChr, cm_id, channel_id);
	return true;
}


//CMSS_CenterMatch_CenterMatchLog = 0x270D,							//比赛游戏记录
bool SERVER_DECL script_world_process_CM_CenterMatchLog(ResourcePtr & sock, WorldPacket & packet)
{
	WSSocketPtr socket = sock;
	char cCode[14];
	if (!ChkSocketPacket(socket, packet, cCode))
		return true;
	CharPtr pChr = socket->getCurrentCharacter();
	if (pChr.isNull())
		return true;

	uint32 cm_id;
	packet >> cm_id;

	// 支持请求偏移量版本
	uint16 offset, num;
	packet >> offset >> num;

	sCenterMatchMgr.GetCenterMatchlogs(pChr, cm_id,offset, num);
	return true;
}

//CMSS_CenterMatch_CenterMatchLoopLog = 0x270E,							//比赛游戏记录
bool SERVER_DECL script_world_process_CM_CenterMatchLoopLog(ResourcePtr & sock, WorldPacket & packet)
{
	WSSocketPtr socket = sock;
	char cCode[14];
	if (!ChkSocketPacket(socket, packet, cCode))
		return true;
	CharPtr pChr = socket->getCurrentCharacter();
	if (pChr.isNull())
		return true;

	uint32 cm_id;
	packet >> cm_id;
	uint32 channelId;
	packet >> channelId;

	// 支持请求偏移量版本
	uint16 offset, num;
	packet >> offset >> num;

	sCenterMatchMgr.GetCenterMatchLooplogs(pChr, cm_id, channelId, 0, offset, num);
	return true;
}

//CMSS_CenterMatch_CenterMatchBattlelog = 0x270F,							//单局比赛游戏记录
bool SERVER_DECL script_world_process_CM_CenterMatchBattlelog(ResourcePtr & sock, WorldPacket & packet)
{
	WSSocketPtr socket = sock;
	char cCode[14];
	if (!ChkSocketPacket(socket, packet, cCode))
		return true;
	CharPtr pChr = socket->getCurrentCharacter();
	if (pChr.isNull())
		return true;

	uint32 cm_id;
	packet >> cm_id;
	uint32 channelId;
	packet >> channelId;
	sCenterMatchMgr.GetCenterMatchBattlelog(pChr, cm_id, channelId);
	return true;
}



//CMSS_CenterMatch_JoinChannel = 0x2724,						//进入牌桌
bool  script_world_process_CM_JoinChannel(ResourcePtr & sock, WorldPacket & packet)
{
	WSSocketPtr socket = sock;
	char cCode[14];
	if (!ChkSocketPacket(socket, packet, cCode))
		return true;

	CharPtr pChr = socket->getCurrentCharacter();
	if (pChr.isNull())
		return true;

	uint32 channel_id;
	packet >> channel_id;


	return true;
}

//GameCommand_CreateCenterMatch = 2000, 	// 后台创建比赛
int script_process_world_game_command_CreateCenterMatch(void * info)
{
	if (!info)
		return SCR_RETURN_ERROR;

	GatewayCommand * command = (GatewayCommand *)info;
	Log.Debug("script_process_world_game_command_CreateCenterMatch", "command id[%u],command_type[%u],command_str[%s]", command->cmdid, command->command_type, (command->parameter).c_str());

	Json::Reader reader;
	Json::Value json_object;
	if (!reader.parse(command->parameter.c_str(), json_object))
		return SCR_RETURN_ERROR;
	CharPtr pChr;
	sCenterMatchMgr.CreateCenterMatch(pChr, command->parameter);

	Log.Notice("script_process_world_game_command_CreateCenterMatch", "");
	return SCR_RETURN_SUCCEED;
}

//GameCommand_SetCenterMatch = 2001, 	// 后台修改比赛
int SERVER_DECL script_process_world_game_command_SetCenterMatch(void * info)
{
	if (!info)
		return SCR_RETURN_ERROR;

	GatewayCommand * command = (GatewayCommand *)info;
	Log.Debug("script_process_world_game_command_SetCenterMatch", "command id[%u],command_type[%u],command_str[%s]", command->cmdid, command->command_type, (command->parameter).c_str());

	Json::Reader reader;
	Json::Value json_object;
	if (!reader.parse(command->parameter.c_str(), json_object))
		return SCR_RETURN_ERROR;
	CharPtr pChr;
	sCenterMatchMgr.ReSetCenterMatch(command->parameter);

	Log.Notice("script_process_world_game_command_SetCenterMatch", "");
	return SCR_RETURN_SUCCEED;
}

//GameCommand_KillCenterMatch = 2002, 	// 后台关闭比赛
int SERVER_DECL script_process_world_game_command_KillCenterMatch(void * info)
{
	if (!info)
		return SCR_RETURN_ERROR;

	GatewayCommand * command = (GatewayCommand *)info;
	Log.Debug("script_process_world_game_command_KillCenterMatch", "command id[%u],command_type[%u],command_str[%s]", command->cmdid, command->command_type, (command->parameter).c_str());

	Json::Reader reader;
	Json::Value json_object;
	if (!reader.parse(command->parameter.c_str(), json_object))
		return SCR_RETURN_ERROR;
	CharPtr pChr;

	uint32 cm_id = 0;
	if (json_object.isMember("cm_id"))
		cm_id = json_object["cm_id"].asUInt();
	CenterMatch cm;
	sCenterMatchMgr.GetCenterMatch(cm_id, &cm);

	uint8 result = 1;
	if (cm.cm_id == 0)
	{
		result = 2;
	}
	if (cm.match_status != enMatchStatus_SignUp)
	{
		result = 3;
	}
	if (cm.status == 255)
	{
		result = 4;
	}
	if(result == 1)
		sCenterMatchMgr.CenterMatchOver(cm.cm_id,enCenterMatchOverType_FZKillMatch);

	Log.Notice("script_process_world_game_command_KillCenterMatch", "result[%d]", result);
	return SCR_RETURN_SUCCEED;
}

// CMSG_REQUIE_MATCHZONELIST = 0x2740,	// 请求赛区列表
bool SERVER_DECL script_world_process_requirematchzonelist(ResourcePtr & sock, WorldPacket & packet)
{
	WSSocketPtr socket = sock;
	char cCode[14];
	if (!ChkSocketPacket(socket, packet, cCode))
		return true;
	
	CharPtr pChr = socket->getCurrentCharacter();
	if (pChr.isNull())
		return true;
	
	Log.Debug("script_world_process_requirematchzonelist","char[%u]", pChr->getHandle());
	sCenterMatchZoneMgr.RequireCenterMatchZoneList(pChr);
	return true;
}

